package com.haierp.task;

import java.util.List;

import com.haierp.service.yitaoItem.YTIOuterItemService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.haierp.enums.GeneralStatus;
import com.haierp.model.Inventory;
import com.haierp.model.InventoryListener;
import com.haierp.service.IInventoryListenerService;
import com.haierp.service.item.IOuterItemService;
import com.haierp.service.xiaohongshu.IXiaohongshuService;

/**
 * 
 * @author zhulu
 * 同步库存异步任务
 *
 */
@Component
public class SynInventoryTask {
	
	protected Logger logger = LogManager.getLogger(getClass());
	@Autowired
	private IOuterItemService outerItemService;
	@Autowired
	private IXiaohongshuService xiaohongshuService;
	@Autowired
	private IInventoryListenerService inventoryListenerService;
	@Autowired
	private YTIOuterItemService ytiOuterItemService;
	@Async
	public void synSkuInventory(Inventory ...inventory){
		if (inventory != null) {
			for (int i = 0; i < inventory.length; i++) {
				try {
					outerItemService.synSkuInventory(inventory[i].getItemId(), inventory[i].getSkuId());
				} catch (Exception e) {
					logger.info("inventory syn error itemId:" + inventory[i].getItemId() + " skuId: "
							+ inventory[i].getSkuId(), e);
				}
			}
		}
	}
	
	/**
	 * 
	 * @param InventoryListener
	 */
	@Async
	public void synSkuInventoryListener(InventoryListener ...inventoryListener){
		if (inventoryListener != null) {
			for (int i = 0; i < inventoryListener.length; i++) {
				try {
					outerItemService.synSkuInventory(inventoryListener[i].getItemId(), inventoryListener[i].getSkuId());
					inventoryListener[i].setStatus(GeneralStatus.SUCCESS.getCode());
					inventoryListenerService.updateById(inventoryListener[i]);
				} catch (Exception e) {
					logger.info("inventory syn error itemId:" + inventoryListener[i].getItemId() + " skuId: "
							+ inventoryListener[i].getSkuId(), e);
				}
			}
		}
	}
	/**
	 *
	 * @param InventoryListener
	 */
	@Async
	public void synSkuInventoryListenerYT(InventoryListener ...inventoryListener){
		if (inventoryListener != null) {
			for (int i = 0; i < inventoryListener.length; i++) {
				try {
					ytiOuterItemService.synSkuInventoryYitao(inventoryListener[i].getItemId(), inventoryListener[i].getSkuId());
					inventoryListener[i].setStatus(GeneralStatus.SUCCESS.getCode());
					inventoryListenerService.updateById(inventoryListener[i]);
				} catch (Exception e) {
					logger.info("inventory syn error itemId:" + inventoryListener[i].getItemId() + " skuId: "
							+ inventoryListener[i].getSkuId(), e);
				}
			}
		}
	}
	/**
	 * 
	 * @param InventoryListener
	 */
	@Async
	public void synXhsSkuInventoryListener(InventoryListener ...inventoryListener){
		if (inventoryListener != null) {
			for (int i = 0; i < inventoryListener.length; i++) {
				try {
					//xiaohongshuService.synXhsSkuInventory(inventoryListener[i].getItemId(), inventoryListener[i].getSkuId());
					inventoryListener[i].setStatus(GeneralStatus.SUCCESS.getCode());
					inventoryListenerService.updateById(inventoryListener[i]);
				} catch (Exception e) {
					logger.info("inventory syn error itemId:" + inventoryListener[i].getItemId() + " skuId: "
							+ inventoryListener[i].getSkuId(), e);
				}
			}
		}
	}
	@Async
	public void batchUpdateInventoryListener(List<InventoryListener> inventorys){
		if(CollectionUtils.isNotEmpty(inventorys)){
			inventoryListenerService.updateBatchById(inventorys);
		}
	}

}
